"use strict";
var share =(function() {
    var stage, ctx, canvas;
    
    var loadedNum = 3;

    var shareSprite = {
        image: null,
        btnclose    : {width:  59, height:115, x: 938, y:  85},
        btnsave     : {width: 307, height: 88, x: 578, y: 164},
        chuwu       : {width: 160, height: 80, x:  2, y: 254},
        hand1       : {width: 448, height: 77, x:  2, y: 175},
        hand2       : {width: 358, height: 77, x: 578, y:  85},
        hand3       : {width: 404, height: 81, x: 578, y:  2},
        p1768       : {width: 173, height: 64, x:  2, y: 336},
        yincaishen  : {width: 574, height:171, x:  2, y:  2}
    };

    var shareSprite2 = {
        image: null,
        btn_share  : {width:322, height:106, x:   2, y: 2},
        tips_1     : {width:233, height: 69, x: 728, y: 2},
        tips_2     : {width:400, height: 69, x: 326, y: 2},
        tips_desc  : {width:295, height: 32, x: 326, y:73},
        qrtips     : {width:135, height: 22, x: 623, y:73}
    }

    var share = {

        init: function() {
            canvas = document.getElementById('canvas');
            canvas.style.width = "5.7rem";
            canvas.style.height = "10.14rem";
            canvas.width = 570;
            canvas.height = 1014;
            stage = this.stage = new createjs.Stage(canvas);
            createjs.Touch.enable(stage);
            ctx = canvas.getContext('2d');
            shareSprite.image = utils.getRes("sharesprite_png");
            shareSprite2.image = utils.getRes("sharesprite2_png");
            canvas.style.display = "none";
            this.render();
        },

        render: function() {
            var bg = new createjs.Bitmap(utils.getRes("share_jpg"));
            stage.addChild(bg);

            var flag1 = this.createSprite("yincaishen", shareSprite).set({y:456});
            var lunar = this.createSprite("chuwu", shareSprite).set({x:228, y:15});

            stage.addChild(lunar, flag1);
            this.drawFortune();
            var data = blessData[meta.figureId];

            var ask = data.ask[meta.blessId-1];
            this.drawBubble(0, ask.text, data.name, ask.nowarp);
            this.drawBubble(1, ask.answer, meta.userName);
            stage.update();
            this.drawLogo();
        },

        drawLogo: function() {
            var self = this;
            var logo = self.logo = this.createSprite("p1768", shareSprite);
            logo.set({ x: 36, y: 919 });
            stage.addChild(logo);

            var qImage = new Image();
            qImage.onload = function() {
                var qcorde = self.qcorde = new createjs.Bitmap(qImage);
                qcorde.x = 418;
                qcorde.y = 888;
                qcorde.scaleX = 91/qImage.width;
                qcorde.scaleY = 91/qImage.height;

                stage.addChild(qcorde);
                stage.update();
                if (--loadedNum===0) self.buildPicture();
            }
            qImage.src = meta.qcordeImage;
        },

        buildPicture: function() {
            var self = this;
            var btnShare = self.btnShare = this.createSprite("btn_share", shareSprite2);
            btnShare.set({ x: 136, y: 895 });

            var qrTips = self.qrTips = this.createSprite("qrtips", shareSprite2).set({x:410, y:983});
            qrTips.set({ x: 407, y: 981 });
            qrTips.scaleX = qrTips.scaleY = 0.8;
            stage.addChild(qrTips);

            canvas.style.display = "block";
            stage.update();
            
            /*var dataurl = canvas.toDataURL("image/png", 0.5);
            var bolb = utils.dataURLtoBlob(dataurl);
            document.getElementById('share').src = utils.getObjectURL(bolb);*/
            document.getElementById('share').src = canvas.toDataURL("image/png", 0.1);
            

            if (flag==1) { // 跳转过来的
                stage.removeChild(self.logo, self.qcorde, self.qrTips);
                stage.addChild(btnShare);
                stage.update();
            } else if (/*utils.isAndroid*/0) {
                var qImage2 = new Image();
                qImage2.onload = function() {
                var _position = $("#canvas").position();
                    $(qImage2).css({
                        position: "absolute",
                        width: "0.97rem",
                        height: "0.97rem",
                        left: (90+417)/100 + "rem",
                        top: Math.max(0, (px2rem($(window).height()) - warpHeight)/2) + 8.91 + "rem",
                        visibility: "hidden"
                    });
                    $("#share").after($(qImage2));
                    setTimeout(function() {
                        $(qImage2).css("visibility", "visible");
                    }, 450);
                }
                qImage2.src = meta.qcordeImage;
            }
            
            self.onComplete();
            
        },

        onComplete: function() {
            var self = this;
            $("#Container").addClass('showed');
            setTimeout(function() {
                var local_tips = "";
                if (localStorage) {
                    local_tips = localStorage.getItem("godwealth_tipstab");
                }
                if (flag!=1) {
                    $("#share").css("opacity", 1);
                    $("#canvas").css("display", "none");
                    $(".btn-join").css("opacity", 1);
                } else {
                    $(".tips_desc").css("visibility", "visible");
                }
                
                if (newUser!="1"&&flag==1) {
                    // self.userGuide();
                } else if (local_tips!="1"&&flag!=1) {
                    // tips 长按二维码
                    var $userGuied = $(".user-guide");
                    $userGuied.addClass('in');
                    $userGuied.addClass('step3');
                    $userGuied.one("click", function() {
                        $userGuied.removeClass('in');
                        setTimeout(function() {
                            $userGuied.remove();
                        }, 400);
                        if (localStorage) localStorage.setItem("godwealth_tipstab", "1");
                    });
                }
            }, 450+50);
            
            var btnShare = self.btnShare;
            if (flag==1) {
                var $userGuide2 = $(".user-guide2");
                var _showGuied2 = function() {
                    $userGuide2.css('display', 'block');
                    setTimeout(function() {
                        $userGuide2.addClass("showed");
                    }, 100);
                }

                var _startime, _endTime;
                $("#share").on("touchstart", function() {
                    _startime = Date.now();
                });
                $("#share").on("touchend", function() {
                    var _now = Date.now();
                    if (_now-500<_endTime) return;
                    _endTime = _now;
                    if (_endTime - _startime<300) { 
                        _showGuied2();
                    }
                });

                self.btnShare.on("click", _showGuied2);

                $userGuide2.on("click", function() {
                    $userGuide2.removeClass("showed");
                    setTimeout(function() {
                        $userGuide2.css('display', 'none');
                    }, 0);
                });
            }
        },
    
        userGuide: function() {
            var self = this;
            var $userGuied = $(".user-guide");
            var step = 0;

            $userGuied.addClass('in');
            setTimeout(function() {
                step = 1;
                stepFn(1);
                $(".user-guide").on("click", stepFn);
            }, 300);


            function stepFn() {
                switch(step++) {
                    case 1:
                        $userGuied.addClass('step1');
                        break;
                    case 2:
                        $userGuied.removeClass('step1').addClass('step2');
                        break;
                    case 3:
                        $(".user-guide").off("click", stepFn);
                        $userGuied.removeClass('in');
                        setTimeout(function() {
                            $userGuied.remove();
                        }, 500);
                        if (localStorage) localStorage.setItem("godwealth_new", "1");
                        break;
                }
            }
        },

        drawFortune: function() {
            var self = this;
            var fImage = new Image();
            fImage.onload = function() {
                var fsprite = new createjs.Bitmap(fImage);
                fsprite.x = (570-fImage.width)/2;
                fsprite.y = 105;
                stage.addChildAt(fsprite,1);
                stage.update();
                if (--loadedNum===0) self.buildPicture();
            }
            fImage.src = utils.path('images/wx/activity/godwealth/avartFigure/'+meta.figureId+'0.png');
        },

        drawBubble: function(type, msg, username, nowarp) {
            var buble = new createjs.Container();
            var askTest, spLen;
            if (!nowarp) {
                var spMsg = utils.sliceStr(msg, 20);
                askTest = spMsg.join("\n");
                spLen = spMsg.length;
            } else {
                askTest = msg;
                spLen = 2;
            }
            var content = new createjs.Text(askTest, "28px Microsoft YaHei", "#000000").set({x: 30, y: 13 });
            var rwidth = (spLen>1)? 330: (content.getMeasuredWidth() + 60);
            var tipsbg = new createjs.Shape();
            
            var nameTips = new createjs.Text(username, "22px Microsoft YaHei", "#505050");
            var bubHeight = 30  + spLen * 34;
            switch(type) {
                case 0:
                    tipsbg.graphics.f('#ffffff').drawRoundRect(0, 0, rwidth, bubHeight, 10)
                        .moveTo(0,20).lineTo(-10, 30).lineTo(0, 40).lineTo(0,20).closePath();
                    buble.set({ x: 173, y: 644 });
                    nameTips.set({x: 173, y: 644-36 });
                    this.drawAvart(utils.getRes("figure_png"), 75, 620);
                    break;
                case 1:
                    tipsbg.graphics.f('#a0e75a').drawRoundRect(0, 0, rwidth, bubHeight, 10)
                        .moveTo(rwidth,20).lineTo(rwidth+10, 30).lineTo(rwidth, 40).lineTo(rwidth,20).closePath();
                    buble.set({ x: 400-rwidth, y: 790 });
                    var nameWidth = nameTips.getMeasuredWidth();
                    nameTips.set({x: 400-nameWidth, y: 790-36 });
                    this.draWecahtAvart(utils.getRes("user_image"), 415, 760);
                    break;
            }
            buble.addChild(tipsbg, content);
            
            stage.addChild(buble, nameTips);
        },

        drawAvart: function(aImage, x, y) {
            var scW = 84/aImage.width, scH = 84/aImage.height;
            var avart = new createjs.Bitmap(aImage);
            avart.scaleX = scW; 
            avart.scaleY = scH;
            var farmeMask = new createjs.Shape();
            farmeMask.graphics.f('#000000').drawRoundRect(x,y,84,84,10);
            avart.mask = farmeMask;
            avart.x = x;
            avart.y = y;
            stage.addChild(avart);
            stage.update();
        },

        draWecahtAvart: function(img, x, y) {
            var self = this;
            var avart = new createjs.Bitmap(img);
            avart.x = x;
            avart.y = y;
            avart.scaleX =84/img.width;
            avart.scaleY =84/img.height;
            var farmeMask = new createjs.Shape();
            farmeMask.graphics.f('#000000').drawRoundRect(x,y,84,84,10);
            avart.mask = farmeMask;
            stage.addChild(avart);
            stage.update();
            if (--loadedNum===0) self.buildPicture();
        },

        createSprite: function(name, spriteStore, props) {
            var self = this;
            var el = new createjs.Bitmap(spriteStore.image);
            el.sourceRect = spriteStore[name];
            return el;
        }

    };

    return share;
})();
